<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import {
  usernameRegisterSelector,
  emailRegisterSelector,
  firstPasswordRegisterSelector,
  secondPasswordRegisterSelector,
  submitRegisterSelector,
  classInvalid,
  classValid,
} from '../../support/commands';

<% const registerPage = clientFrameworkVue ? '/register' : '/account/register'; _%>
describe('<%= registerPage %>', () => {
  beforeEach(() => {
    cy.visit('<%= registerPage %>');
  });

  beforeEach(() => {
    cy.intercept('POST', '/api/register').as('registerSave');
  });

  it('should be accessible through menu', () => {
    cy.visit('');
    cy.clickOnRegisterItem();
    cy.url().should('match', /<%= registerPage.replace(/\//g, '\\/') %>$/);
  });

  it('should load the register page', () => {
    cy.get(submitRegisterSelector).should('be.visible');
  });

  it('requires username', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(usernameRegisterSelector).should('have.class', classInvalid);
    cy.get(usernameRegisterSelector).type('test');
    cy.get(usernameRegisterSelector).blur();
    cy.get(usernameRegisterSelector).should('have.class', classValid);
  });

  it('should not accept invalid email', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(emailRegisterSelector).should('have.class', classInvalid);
    cy.get(emailRegisterSelector).type('testtest.fr');
    cy.get(emailRegisterSelector).blur();
    cy.get(emailRegisterSelector).should('have.class', classInvalid);
  });

  it('requires email in correct format', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(emailRegisterSelector).should('have.class', classInvalid);
    cy.get(emailRegisterSelector).type('test@test.fr');
    cy.get(emailRegisterSelector).blur();
    cy.get(emailRegisterSelector).should('have.class', classValid);
  });

  it('requires first password', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(firstPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(firstPasswordRegisterSelector).type('test@test.fr');
    cy.get(firstPasswordRegisterSelector).blur();
    cy.get(firstPasswordRegisterSelector).should('have.class', classValid);
  });

  it('requires password and confirm password to be same', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(firstPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(firstPasswordRegisterSelector).type('test');
    cy.get(firstPasswordRegisterSelector).blur();
    cy.get(firstPasswordRegisterSelector).should('have.class', classValid);
    cy.get(secondPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(secondPasswordRegisterSelector).type('test');
    cy.get(secondPasswordRegisterSelector).blur();
    cy.get(secondPasswordRegisterSelector).should('have.class', classValid);
  });

  it('requires password and confirm password have not the same value', () => {
    <%_ if (clientFrameworkReact) { _%>
    cy.get(submitRegisterSelector).click();
    <%_ } _%>
    cy.get(firstPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(firstPasswordRegisterSelector).type('test');
    cy.get(firstPasswordRegisterSelector).blur();
    cy.get(firstPasswordRegisterSelector).should('have.class', classValid);
<%_ if (clientFrameworkAngular) { _%>
    cy.get(secondPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(secondPasswordRegisterSelector).type('otherPassword');
    cy.get(submitRegisterSelector).should('be.disabled');
<%_ } else { _%>
    cy.get(secondPasswordRegisterSelector).should('have.class', classInvalid);
    cy.get(secondPasswordRegisterSelector).type('otherPassword');
    cy.get(secondPasswordRegisterSelector).blur();
    cy.get(secondPasswordRegisterSelector).should('have.class', classInvalid);
<%_ } _%>
  });

  it('register a valid user', () => {
    const randomEmail = '<%= faker.internet.email() %>';
    const randomUsername = '<%= faker.internet.username() %>';
    cy.get(usernameRegisterSelector).type(randomUsername);
    cy.get(emailRegisterSelector).type(randomEmail);
    cy.get(firstPasswordRegisterSelector).type('jondoe');
    cy.get(secondPasswordRegisterSelector).type('jondoe');
    cy.get(submitRegisterSelector).click();
    cy.wait('@registerSave').then(({ response }) => expect(response?.statusCode).to.equal(201));
  });
});
